//
//  CategoryViewController.m
//  帮惠Mall
//
//  Created by reew on 2017/9/22.
//  Copyright © 2017年 reew. All rights reserved.
//

#import "CategoryViewController.h"
#import "CommonUtils.h"
#import <AFNetworking.h>
#import "Admin.h"
#import "GoodsType.h"
#import "AddGoodsTableViewController.h"
#import "System.h"
@interface CategoryViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *leftTableView;
@property (weak, nonatomic) IBOutlet UITableView *rightTableView;
@property (nonatomic,strong) NSMutableArray *leftData;
@property (nonatomic,strong) NSMutableArray *rightData;
@property (nonatomic,strong) NSString *leftCategory;
@end

@implementation CategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置两个tableView
    self.tabBarController.tabBar.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setUpTableView];
    [self loadDataWithTypeNo:nil];
}
- (void)setUpTableView{
    //左
    _leftTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    _leftTableView.delegate = self;
    _leftTableView.dataSource = self;
    _leftTableView.tag = 1000;
    _leftTableView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [_leftTableView.layer setMasksToBounds:YES];
    //右
    _rightTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    _rightTableView.delegate = self;
    _rightTableView.dataSource = self;
    _rightTableView.tag = 1001;
    _rightTableView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [_rightTableView.layer setMasksToBounds:YES];
}
- (void)loadDataWithTypeNo:(NSString *)typeNo{
    NSString *url = [kBaseURL stringByAppendingPathComponent:kQuery_goods_type];
//    @"http://192.168.1.112:8080/bang_mall/mall/query_goods_type";
    //
    AFHTTPSessionManager *manager = [CommonUtils getAFHTTPSessionManager];
    [manager POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        //参数typeNo（商品父类型编号）
        if(typeNo != nil){
            [formData appendPartWithFormData:[typeNo dataUsingEncoding:NSUTF8StringEncoding] name:@"typeNo"];
        }else{
            [formData appendPartWithFormData:[@"" dataUsingEncoding:NSUTF8StringEncoding] name:@"typeNo"];
        }
        
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *goodsType = responseObject[@"data"][@"goodsType"];
        if (typeNo == nil) {
            //总类目
            _leftData = [NSMutableArray array];
            for (NSDictionary *dic in goodsType) {
                GoodsType *type = [[GoodsType alloc] initGoodTypeModelWithDictionary:dic];
                [_leftData addObject:type];
            }
            [_leftTableView reloadData];
            
        }else{
            //子类目
            _rightData = [NSMutableArray array];
            for (NSDictionary *dic in goodsType) {
                GoodsType *type = [[GoodsType alloc] initGoodTypeModelWithDictionary:dic];
                [_rightData addObject:type];
            }
            [_rightTableView reloadData];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView.tag == 1000) {
        return _leftData.count;
    }else{
        return  _rightData.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
    if (tableView.tag == 1000) {
        GoodsType *type = _leftData[indexPath.row];
        cell.textLabel.text = type.name;
    }else{
        GoodsType *type = _rightData[indexPath.row];
        cell.textLabel.text = type.name;
    }
    
    return cell;
}
#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (tableView.tag == 1000) {
        GoodsType *type1 = _leftData[indexPath.row];
        [self loadDataWithTypeNo:type1.typeNo];
        _leftCategory = type1.name;
    }else{
        GoodsType *type2 = _rightData[indexPath.row];
        NSUInteger index = self.navigationController.viewControllers.count-2;
        AddGoodsTableViewController *vc = self.navigationController.viewControllers[index];
        vc.typeNo = type2.typeNo;
        vc.typeCell.detailTextLabel.text = [NSString stringWithFormat:@"%@-%@",_leftCategory,type2.name];
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}


@end
